/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.component;

import java.util.HashMap;
import java.util.Map;

import com.cleangwt.bootstrap.client.ui.Dropdown;
import com.cleangwt.bootstrap.client.ui.DropdownBuilder;
import com.cleangwt.bootstrap.client.ui.Modal;
import com.cleangwt.showcase.client.SourceProvider;
import com.cleangwt.user.client.AnchorActionEvent;
import com.cleangwt.user.client.AnchorActionEvent.AnchorActionHandler;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.web.client.Requests;
import com.cleangwt.web.client.StatedCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;

/**
 * The source viewer for retrieving source code.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class PageHeader extends Composite {
  private static boolean sourceGenerated;

  private Dropdown sourceList = new Dropdown("Souce Code");
  private Map<String, String> sourceMap = new HashMap<String, String>();

  public PageHeader(String caption, String desc, String sources) {
    initWidget(Lay.p(Lay.raw("blockquote", "<h4>" + caption + "</h4><p>" + desc + "</p>"),
        sourceList));
    sourceList.getElement().getStyle().setPaddingRight(72, Unit.PX);
    setSources(sources);
    sourceList.addAnchorActionHandler(new AnchorActionHandler() {

      @Override
      public void onAnchorAction(AnchorActionEvent event) {
        if (event.getAction().equals("viewsource")) {
          viewSource(event.getParameters()[0]);
        }
      }
    });
  }

  private void retrieveSource(final String source) {
    // generate once
    if (!sourceGenerated) {
      sourceGenerated = true;
      GWT.create(SourceProvider.class);
    }

    Requests.get(GWT.getModuleBaseURL() + SourceProvider.GENERATED_SOURCE_DIR + source + ".html",
        new StatedCallback() {

          @Override
          protected void onSuccess(String result) {
            sourceMap.put(source, result);
            viewSource(source);
          }
        });
  }

  public void setSources(String sources) {
    if (sources != null && !sources.isEmpty()) {
      DropdownBuilder builder = sourceList.build();
      for (String source : sources.split("\\s+")) {
        builder.addLink(source, "#@viewsource/" + source);
      }
    }
  }

  public void viewSource(String source) {
    if (sourceMap.containsKey(source)) {
      HTML html = new HTML(sourceMap.get(source));
      Modal m = new Modal(true, true);
      m.setTitle(source);
      m.setBody(html);
      m.setDialogWidth(860);
      m.show();
    } else {
      retrieveSource(source);
    }
  }
}
